package com.haixue.android.academy.view.tiku;

import android.academy.haixue.com.haixue.R;
import android.app.Activity;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileNotFoundException;

import uk.co.senab.photoview.PhotoViewAttacher;
import uk.co.senab.photoview.PhotoViewAttacher.OnMatrixChangedListener;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;


public class ImageActivity extends Activity{
	  private TextView mCurrMatrixTv;
	  private PhotoViewAttacher mAttacher;
	  private  ImageView mImageView;
	  private static float left=0f;
	   @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.image_activity);

	         mImageView = (ImageView) findViewById(R.id.iv_photo);
	     //   Button close = (Button) findViewById(R.id.iv_imageclose);
	       // close.setImageDrawable(getResources().getDrawable(R.drawable.image_close));
	       // mCurrMatrixTv = (TextView) findViewById(R.id.tv_current_matrix);
	        Uri uri=getIntent().getData();
	      //  Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
	       // Drawable bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
	        Drawable bitmap = null;
			try {
				bitmap = Drawable.createFromStream(this.getContentResolver().openInputStream(uri), null);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        mImageView.setImageDrawable(bitmap);
              //  mImageView.SET
	        // The MAGIC happens here!
	        mAttacher = new PhotoViewAttacher(mImageView);
	        mAttacher.setOnMatrixChangeListener(new MatrixChangeListener());
	        mAttacher.setOnPhotoTapListener(new PhotoTapListener());
	       /* mAttacher.setOnPhotoTapListener(new OnPhotoTapListener() {
				
				@Override
				public void onPhotoTap(View view, float x, float y) {
					// TODO Auto-generated method stub
					mAttacher.setScale(1.0f);
					//finish();
				}
			});*/
             
	        // Lets attach some listeners, not required though!
	      /*  mAttacher.setOnMatrixChangeListener(new MatrixChangeListener());
	        mAttacher.setOnPhotoTapListener(new PhotoTapListener());*/
	        
	    }
	   private class PhotoTapListener implements OnPhotoTapListener {

	        @Override
	        public void onPhotoTap(View view, float x, float y) {
	           if(left<0){
	        	   mAttacher.setScale(1.0f);
	           }else{
	        	   
	        	  finish();
	        	 // overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit); 
	        	  //overridePendingTransition(R.anim.fade, R.anim.hode); 
	        	  overridePendingTransition(0, 0); 
	           }
	        }
	    }

	 

	    private class MatrixChangeListener implements OnMatrixChangedListener {

	        @Override
	        public void onMatrixChanged(RectF rect) {
	        	left=rect.left;
	        }
	    }
	    @Override  
	    public boolean onKeyDown(int keyCode, KeyEvent event) {  
	        if (keyCode == KeyEvent.KEYCODE_BACK ) {  
	            //showDialog(APP_EXIT);
	        	
	        	
	        	finish();
	        //	overridePendingTransition(R.anim.fade, R.anim.hode); 
	        	overridePendingTransition(0, 0); 
	        	 //overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit); 
	        	
	            return false;  
	        } else  
	            return super.onKeyDown(keyCode, event);  
	    }  

}
